//===============================================================================
// Microsoft patterns & practices
// Guidance Explorer Offline Client
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================

using System;
using System.Collections.Generic;

namespace GuidanceExplorer.Model
{
    public class GuidanceItemCollection : Dictionary<string, GuidanceItem>, IModelItem, IGuidanceItemContainer
    {
        /// <summary>
        /// Factory.
        /// </summary>
        /// <returns>A new instance of the GuidanceItemCollection</returns>
        public static GuidanceItemCollection New()
        {
            return new GuidanceItemCollection();
        }

        /// <summary>
        /// The columns for this collection.
        /// </summary>
        private ColumnCollection _columns;

        /// <summary>
        /// Default private constructor.
        /// </summary>
        private GuidanceItemCollection() : base( StringComparer.InvariantCultureIgnoreCase ) { }

        public override bool Equals(object obj)
        {
            GuidanceItemCollection other = obj as GuidanceItemCollection;
            if (other != null)
            {
                if (this.Count != other.Count) return false;
                IEnumerator<GuidanceItem> _thisCollection = this.Values.GetEnumerator();
                IEnumerator<GuidanceItem> _otherCollection = other.Values.GetEnumerator();
                while (_thisCollection.MoveNext() && _otherCollection.MoveNext())
                {
                    if (!_thisCollection.Current.Equals(_otherCollection.Current))
                    {
                        return false;
                    }
                }
                return true;
            }
            else
            {
                return base.Equals(obj);
            }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// Finds an item by using it's Id.
        /// </summary>
        /// <param name="guidanceItemId"></param>
        /// <returns></returns>
        public GuidanceItem FindItemById(string guidanceItemId)
        {
            if (ContainsKey(guidanceItemId))
            {
                return this[guidanceItemId];
            }
            return null;
        }

        #region IModelItem Members

        /// <summary>
        /// This item is not an editable model item.
        /// </summary>
        public bool IsEditable
        {
            get { return false; }
        }

        /// <summary>
        /// Nothing can be dropped here.
        /// </summary>
        /// <param name="modelItem"></param>
        /// <returns></returns>
        public bool CanDrop(IModelItem modelItem)
        {
            return false;
        }

        #endregion

        #region IGuidanceItemContainer Members

        /// <summary>
        /// Return the columns for the guidance view.
        /// </summary>
        public ColumnCollection Columns
        {
            get
            {
                if (_columns == null)
                {
                    _columns = ColumnCollection.DefaultColumns(GuidanceType.New(GuidanceType.DefaultTypeName));
                }
                return _columns;
            }
            set
            {
                _columns = value;
            }
        }

        public GuidanceItemCollection Items
        {
            get { throw new Exception("The method or operation is not implemented."); }
            set { throw new Exception("The method or operation is not implemented."); }
        }

        public bool HasItems
        {
            get { return false; }
        }

        public bool CanSetItems
        {
            get { return false; }
        }

        #endregion

        #region IEnumerable<GuidanceItem> Members

        public new IEnumerator<GuidanceItem> GetEnumerator()
        {
            return this.Values.GetEnumerator();
        }

        #endregion

        public GuidanceItemFileReference GetItemByPath(string path)
        {
            foreach (GuidanceItemFileReference item in this)
            {
                if (item.FileName == path)
                {
                    return item;
                }
            }

            return null;
        }

        public GuidanceItemCollection Clone()
        {
            GuidanceItemCollection items = new GuidanceItemCollection();
            foreach (GuidanceItem item in this)
            {
                items.Add(item.Id, item);
            }

            return items;
        }
    }
}

